package GUI;

import Model.Product;

import javax.swing.table.DefaultTableModel;
import java.util.ArrayList;

public class SearchResultsTableModel extends DefaultTableModel {

    private ArrayList<Product> data;

    @Override
    public boolean isCellEditable(int row, int column) {
        return false;
    }

    public void setData(ArrayList<Product> data) {
        this.data = data;
        fireTableDataChanged();
    }

    public ArrayList<Product> getData() {
        return data;
    }

    @Override
    public int getColumnCount() {
        return 6;
    }

    @Override
    public int getRowCount() {
        if(data == null) {
            return 0;
        }
        else {
            return data.size();
        }
    }

    @Override
    public String getColumnName(int column) {
        String columnName = "";
        switch (column) {
            case 0:
                columnName ="ID";
                break;
            case 1:
                columnName = "Name";
                break;
            case 2:
                columnName = "Sales price";
                break;
            case 3:
                columnName = "Rent price";
                break;
            case 4:
                columnName = "Country of origin";
                break;
            case 5:
                columnName = "Type";
                break;
        }
        return columnName;
    }

    @Override
    public Object getValueAt(int row, int column) {
        Object value = null;
        switch (column) {
            case 0:
                value = data.get(row).getId();
                break;
            case 1:
                value = data.get(row).getName();
                break;
            case 2:
                value = data.get(row).getPurchasePrice();
                break;
            case 3:
                value = data.get(row).getSalesPrice();
                break;
            case 4:
                value = data.get(row).getRentPrice();
                break;
            case 5:
                value = data.get(row).getCountryOfOrigin();
                break;
            case 6:
                value = data.get(row).getMinStock();
                break;
            case 7:
                value = data.get(row).getType();
                break;
            case 8:
                value = data.get(row).getSupplier();
                break;

        }

        return value;
    }

}
